'''# built_in'''
from datetime import datetime

'''# custom'''
from utils.public_Script import isLogicMethodClass


class CellValueHandle:

    def __repr__(self) -> str:
        return '用例步骤文件，各步骤行单元格值获取并处理'

    def __init__(self) -> None:
        self.WS_Object = None
        self.startRow = None
        self.elementExpression = None
        self.tagging = None  # 步骤说明
        self.keywordName = None  # 指定的关键字
        self.positionInformation = None  # 元素位置信息
        self.manipulatedValue = None  # 函数操作值
        self.executionTime= None  # 用例执行时间
        self.logicFunction_Object = isLogicMethodClass()  # 实例化公共脚本对象

    def __call__(self, WS_Object, startRow, elementExpression) -> None:
        self.WS_Object = WS_Object
        self.startRow = startRow
        self.elementExpression = elementExpression

        '''读出步骤行单元格值'''
        self.__get_Value()

        '''处理步骤行单元格值'''
        self.__handle_Value()

        '''返还步骤行单元格值'''
        return {
            'tagging': self.tagging,
            'keywordName': self.keywordName,
            'positionInformation': self.positionInformation,
            'manipulatedValue': self.manipulatedValue,
            'executionTime': self.executionTime
        }

    def __get_Value(self) -> None:
        self.tagging = self.WS_Object.cell(row=self.startRow, column=3).value  # 步骤说明
        self.keywordName = self.WS_Object.cell(row=self.startRow, column=4).value  # 指定的关键字
        self.positionInformation = self.WS_Object.cell(row=self.startRow, column=5).value  # 元素操作
        self.manipulatedValue = self.WS_Object.cell(row=self.startRow, column=6).value  # 函数操作值
        self.executionTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 步骤运行时间

    def __handle_Value(self) -> None:
        # positionInformation 值处理
        if self.positionInformation != None:
            try: 
                node_Key = self.positionInformation.split('/')
                self.positionInformation = self.elementExpression[node_Key[0]][node_Key[1]]
            except Exception as error:
                self.WS_Object.cell(row=self.startRow, column=5).value = str(self.WS_Object.cell(row=self.startRow, column=5).value) + '\n该行元素操作关键字书写错误！'

        # manipulatedValue 值处理
        try:
            self.manipulatedValue.strip()[:10]  # string类型且不为空字符
        except Exception as error:
            pass
        else:
            '''操作内容预处理'''
            self.manipulatedValue = self.logicFunction_Object.beforehand(self.manipulatedValue)